using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Service.Interfaces;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.DALProxy.Extensions;

namespace ACME.Northwind.DALProxy
{
	/// <summary>
	/// The service implementation for Territory object
	/// </summary>
	public partial class TerritoryService : ITerritoryService
	{
		#region ITerritoryService Members

		/// <summary>
		/// Run a selection by the primary key
		/// </summary>
		public TerritoryDTO SelectByPrimaryKey(string territoryid)
		{
			TerritoryDTO retVal = new TerritoryDTO();
			if (!retVal.SelectUsingPK(territoryid)) return null;
			else return retVal;
		}

		/// <summary>
		/// Run the persist for an object
		/// </summary>
		public bool Persist(TerritoryDTO dto)
		{
			return (dto.Persist() != null);
		}

		/// <summary>
		/// Run the persist for a list
		/// </summary>
		public bool Persist(List<TerritoryDTO> list)
		{
			return list.Persist();
		}

		/// <summary>
		/// Run a selection for all objects
		/// </summary>
		public List<TerritoryDTO> RunSelect()
		{
			List<TerritoryDTO> retVal = new List<TerritoryDTO>();
			retVal.RunSelect();
			return retVal;
		}

		/// <summary>
		/// Run a selection using LINQ
		/// </summary>
		public List<TerritoryDTO> RunSelect(string linq)
		{
			List<TerritoryDTO> retVal = new List<TerritoryDTO>();
			retVal.RunSelect(linq);
			return retVal;
		}

		/// <summary>
		/// Run a paginated selection using LINQ
		/// </summary>
		public PagedQueryResults<TerritoryDTO> RunSelect(int pageIndex, int pageSize, bool ascending, string sortColumn, string linq)
		{
			List<TerritoryDTO> list = new List<TerritoryDTO>();
			string sortOrder;
			if (ascending)
				sortOrder = "ascending";
			else
				sortOrder = "descending";
			return list.RunSelect(pageSize.ToString(), pageIndex.ToString(), sortOrder, sortColumn, linq);
		}

		/// <summary>
		/// Return a list of related EmployeeTerritory objects
		/// </summary>
		public List<EmployeeTerritoryDTO> GetEmployeeTerritoryList(TerritoryDTO item)
		{
			List<EmployeeTerritoryDTO> retval = new List<EmployeeTerritoryDTO>();
			retval.SelectByTerritory(item.TerritoryId);
			return retval;
		}

		/// <summary>
		/// Return the related Region item
		/// </summary>
		public RegionDTO GetRegionItem(TerritoryDTO item)
		{
			RegionDTO retval = new RegionDTO();
			retval.SelectUsingPK(item.RegionId);
			return retval;
		}

		/// <summary>
		/// Delete the specified item
		/// </summary>
		public bool Delete(TerritoryDTO item)
		{
			Territory territory = Territory.SelectUsingPK(item.TerritoryId);
			if (territory == null) return false;
			territory.Delete();
			territory.Persist();
			return true;
		}

		#endregion

	}
}
